#define _CRT_SECURE_NO_WARNINGS 1

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
 /**
  * Note: The returned array must be malloced, assume caller calls free().
  */
int returnsize(struct TreeNode* root)
{
    if (root == NULL)
        return 0;
    return returnsize(root->left) + returnsize(root->right) + 1;
}

void pushA(struct TreeNode* root, int* a, int* i)
{
    if (root == NULL)
        return;

    a[(*i)++] = root->val;

    pushA(root->left, a, i);
    pushA(root->right, a, i);
}

int* preorderTraversal(struct TreeNode* root, int* returnSize) {
    *returnSize = returnsize(root);
    int* a = (int*)malloc(sizeof(int) * *returnSize);
    int i = 0;

    pushA(root, a, &i);

    return a;
}